package com.jt.redis;

import redis.clients.jedis.Jedis;

import java.util.Map;

public class CartDemo01 {
    static final String IP="192.168.126.129";
    static final int PORT= 6379;
    public static void main(String[] args) {
        String userId = "1001";
        doAddCart(userId, "201", 1);
        doAddCart(userId, "202", 1);
        doAddCart(userId, "203", 1);
        doAddCart(userId, "202", 1);
        doAddCart(userId, "203", 2);
        Map<String, String> cart = doViewCart(userId);
        System.out.println(cart);
        doClearCart(userId,"201","202","203");
        doDelCart(userId, "203");
        cart = doViewCart(userId);
        System.out.println(cart);

    }

    public static void doClearCart(String userId, String... productId) {
        Map<String, String> cart = doViewCart(userId);
        System.out.println(cart);
    }

    public static void doAddCart(String userId,String productId,int num){
        Jedis jedis = new Jedis(IP, PORT);
//        jedis.hset(, );
        jedis.hincrBy("cart:"+userId, productId, num);
        jedis.close();
    }
    public static Map<String, String> doViewCart(String userId){
        Jedis jedis = new Jedis(IP, PORT);
        Map<String, String> cart = jedis.hgetAll("cart:" + userId);
        jedis.close();
        return cart;
    }
    public static void doDelCart(String userId,String productId){
        Jedis jedis = new Jedis(IP, PORT);
        jedis.hdel("cart:"+userId, productId);
        jedis.close();

    }



}
